﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SnongPhoneShop.Models;

namespace SnongPhoneShop.Controllers
{
    public class AjaxController : Controller
    {
        //
        // GET: /Ajax/

        StoredProcedure sp = new StoredProcedure();

        [HttpGet]
        public JsonResult GetListCatalogByTypeID(int? id)
        {
            if (id != null)
            {
                var L = (from Lc in sp.GetListCatalogByTypeID((int)id)
                         select new
                         {
                             MaDanhMuc = Lc.MaDanhMuc,
                             TenDanhMuc = Lc.TenDanhMuc
                         }).ToList();
                return Json(L, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var L = (from Lc in sp.GetListCatalogByTypeID(0)
                         select new
                         {
                             MaDanhMuc = Lc.MaDanhMuc,
                             TenDanhMuc = Lc.TenDanhMuc
                         }).ToList();
                return Json(L, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpGet]
        public JsonResult GetListPhoneByCatalogID(int ID_DM, string ID_SP)
        {
            try
            {
                var ListPhone = sp.GetListProductByCatalogID(ID_DM).Where(m => !m.MaSanPham.Equals(ID_SP)).ToList();
                List<SanPham> L = new List<SanPham>();
                foreach (var Item in ListPhone)
                {
                    SanPham SP = new SanPham();
                    SP.MaSanPham = Item.MaSanPham;
                    SP.TenSanPham = Item.TenSanPham;
                    L.Add(SP);
                }
                return Json(L, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpGet]
        public JsonResult GetPhoneByID(string ID_SP)
        {
            var SP = sp.GetProductByID(ID_SP);
            string JackAudio, LoaNgoai, GPRS, EDGE, C3G, GPS, Bluetooth, Wifi, HongNgoai, Radio, HoTroJava, HoTroNhieuSim;
            JackAudio = LoaNgoai = GPRS = EDGE = C3G = GPS = Bluetooth = Wifi = HongNgoai = Radio = HoTroJava = HoTroNhieuSim = "Không";
            if (SP.JackAudio == true)
            {
                JackAudio = "Có";
            }
            if (SP.LoaNgoai == true)
            {
                LoaNgoai = "Có";
            }
            if (SP.EDGE == true)
            {
                EDGE = "Có";
            }
            if (SP.GPRS == true)
            {
                GPRS = "Có";
            }
            if (SP.C3G == true)
            {
                C3G = "Có";
            }
            if (SP.GPS == true)
            {
                GPS = "Có";
            }
            if (SP.Bluetooth == true)
            {
                Bluetooth = "Có";
            }
            if (SP.Wifi == true)
            {
                Wifi = "Có";
            }
            if (SP.HongNgoai == true)
            {
                HongNgoai = "Có";
            }
            if (SP.Radio == true)
            {
                Radio = "Có";
            }
            if (SP.HoTroJava == true)
            {
                HoTroJava = "Có";
            }
            if (SP.HoTroNhieuSim == true)
            {
                HoTroNhieuSim = "Có";
            }
            return Json(new
            {
                TenSanPham = SP.TenSanPham,
                AnhDaiDien = SP.AnhDaiDien != null ? SP.AnhDaiDien : "#",
                DonGia = SP.DonGia.ToString("n0") + " VNĐ",
                TinhNangNoiBat = SP.TinhNangNoiBat != null ? SP.TinhNangNoiBat : "",
                TrangThaiPhatHanh = SP.TrangThaiPhatHanh != null ? SP.TrangThaiPhatHanh.TenTrangThai : "",
                NgayPhatHanh = SP.NgayPhatHanh != null ? SP.NgayPhatHanh.Value.ToShortDateString() : "",
                BangTan = SP.BangTan != null ? SP.BangTan : "",
                ChieuDai = SP.ChieuDai != null ? SP.ChieuDai.ToString() : "",
                ChieuRong = SP.ChieuRong != null ? SP.ChieuRong.ToString() : "",
                ChieuCao = SP.ChieuCao != null ? SP.ChieuCao.ToString() : "",
                TrongLuong = SP.TrongLuong != null ? SP.TrongLuong.ToString() : "",
                KieuManHinh = SP.KieuManHinh != null ? SP.KieuManHinh.TenKieuManHinh : "",
                KichThuocManHinh = SP.KichThuocManHinh != null ? SP.KichThuocManHinh : "",
                DoPhanGiai = SP.DoPhanGiai != null ? SP.DoPhanGiai : "",
                CamUng = SP.CamUng != null ? SP.CamUng.TenCamUng : "",
                TinhNangKhac = SP.TinhNangKhac != null ? SP.TinhNangKhac : "",
                AmBao = SP.AmBao != null ? SP.AmBao : "",
                Radio = Radio,
                LoaNgoai = LoaNgoai,
                DanhBaLuuTru = SP.DanhBaLuuTru != null ? SP.DanhBaLuuTru : "",
                BoNhoTrong = SP.BoNhoTrong != null ? SP.BoNhoTrong : "",
                HoTroTheNho = SP.HoTroTheNho != null ? SP.HoTroTheNho : "",
                GPRS = GPRS,
                EDGE = EDGE,
                C3G = C3G,
                TrinhDuyetWeb = SP.TrinhDuyetWeb != null ? SP.TrinhDuyetWeb : "",
                GPS = GPS,
                Bluetooth = Bluetooth,
                Wifi = Wifi,
                HongNgoai = HongNgoai,
                USB = SP.USB != null ? SP.USB : "",
                MayAnhChinh = SP.MayAnhChinh != null ? SP.MayAnhChinh : "",
                MayAnhPhu = SP.MayAnhPhu != null ? SP.MayAnhPhu : "",
                DinhDangNhac = SP.DinhDangNhac != null ? SP.DinhDangNhac : "",
                DinhDangPhim = SP.DinhDangPhim != null ? SP.DinhDangPhim : "",
                JackAudio = JackAudio,
                UngDung = SP.UngDung != null ? SP.UngDung : "",
                HoTroJava = HoTroJava,
                HoTroNhieuSim = HoTroNhieuSim,
                KieuDang = SP.KieuDang != null ? SP.KieuDang.TenKieuDang : "",
                HeDieuHanh = SP.HeDieuHanh != null ? SP.HeDieuHanh : "",
                Chip = SP.Chip != null ? SP.Chip : "",
                RAM = SP.RAM != null ? SP.RAM : "",
                NgonNgu = SP.NgonNgu != null ? SP.NgonNgu : "",
                LoaiBanPhim = SP.LoaiBanPhim != null ? SP.LoaiBanPhim.TenLoaiBanPhim : "",
                Pin = SP.Pin != null ? SP.Pin : ""
            }, JsonRequestBehavior.AllowGet);
        }

        [ChildActionOnly]
        private ActionResult DS_Tim_Kiem_Nhanh(string Name)
        {
            return PartialView(sp.GetListProductByName(Name));
        }

        [HttpGet]
        public ActionResult GetQuickResult(string Name)
        {
            return PartialView("DS_Tim_Kiem_Nhanh", sp.GetListProductByName(Name));
        }

        [ChildActionOnly]
        public ActionResult Binh_Luan(string ID)
        {
            ViewBag.MaSanPham = ID;
            return PartialView(sp.GetListCommentOfProductByID(ID));
        }

        [HttpGet]
        public ActionResult Gui_Binh_Luan(string txt, string ID_SP)
        {
            sp.AddComment(txt, ID_SP, Session["User"].ToString());
            return PartialView("Binh_Luan", sp.GetListCommentOfProductByID(ID_SP));
        }

        [HttpGet]
        public ActionResult Xoa_Binh_Luan(int ID_Cmt, string ID_SP)
        {
            sp.DeleteComment(ID_Cmt);
            return PartialView("Binh_Luan", sp.GetListCommentOfProductByID(ID_SP));
        }

        [HttpGet]
        public void SetCookieColor(string Selector, string Color)
        {
            HttpCookie newCookie;
            if (Request.Cookies["Color"] == null) newCookie = new HttpCookie("Color");
            else newCookie = Request.Cookies["Color"];
            newCookie.Values.Remove(Selector);
            newCookie.Values.Add(Selector, Color);
            newCookie.Expires = DateTime.Now.AddDays(10);
            Response.Cookies.Add(newCookie);
        }
    }
}
